{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "9c7fe508",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0b111'"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "bytes(7)\n",
    "\n",
    "bin(7)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3342c3fc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Numbers: 4, 5, 6\n",
      "Numbers: 4, 1, 2\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "nums = [4, 5, 6]\n",
    "msg = \"Numbers: {0}, {1}, {2}\".format(nums[0], nums[1], nums[2])\n",
    "print(msg)\n",
    "print(f'Numbers: {nums[0]}, {1}, {2}')\n",
    "print('%d' % nums[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "fdff12cf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "True\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "34"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ages = {\"Dave\": 24, \"Mary\":42, \"John\":58}\n",
    "print(\"Mary\" in ages)      # True\n",
    "print(\"Will\" not in ages)  # Trueages.get(\"Dave\")\n",
    "ages.get(\"Hurry\")          # 返回None\n",
    "ages.get(\"Hurry\", 34)      # 返回34"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "7130355a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1024 * 768 =  786432\n"
     ]
    }
   ],
   "source": [
    "print(\"1024 * 768 = \", 1024 * 768)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "1ec680bc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm learning\n",
      "Python.\n",
      "I'm learning \n",
      "Python\n"
     ]
    }
   ],
   "source": [
    "print('I\\'m learning\\nPython.')\n",
    "print(\"I'm learning \\nPython\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "575fadaf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a =  [1, 2, 3] \tid(a) =  2342596529728\n",
      "b =  [1, 2, 3] \tid(b) =  2342596529728\n",
      "a =  [1, 2, 3, 4] \tid(a) =  2342596529728\n",
      "b =  [1, 2, 3, 4] \tid(b) =  2342596529728\n"
     ]
    }
   ],
   "source": [
    "# 浅拷贝\n",
    "a = [1, 2, 3]\n",
    "b = a\n",
    "print('a = ', a, \"\\tid(a) = \", id(a))\n",
    "print('b = ', b, \"\\tid(b) = \", id(b))\n",
    "a.append(4)\n",
    "print('a = ', a, \"\\tid(a) = \", id(a))\n",
    "print('b = ', b, \"\\tid(b) = \", id(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "cb5fd4b3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a =  [1, 2, 3] \tid(a) =  2342568161664\n",
      "b =  [1, 2, 3] \tid(b) =  2342568161664\n",
      "a =  [2, 3, 4] \tid(a) =  2342596224640\n",
      "b =  [1, 2, 3] \tid(b) =  2342568161664\n"
     ]
    }
   ],
   "source": [
    "a = [1, 2, 3]\n",
    "b = a\n",
    "print('a = ', a, \"\\tid(a) = \", id(a))\n",
    "print('b = ', b, \"\\tid(b) = \", id(b))\n",
    "a = [2, 3 , 4]\n",
    "print('a = ', a, \"\\tid(a) = \", id(a))\n",
    "print('b = ', b, \"\\tid(b) = \", id(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "4bb71407",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a =  str \tid(a) =  2342483282544\n",
      "b =  str \tid(b) =  2342483282544\n",
      "a =  str \tid(a) =  2342483282544\n",
      "b =  str \tid(b) =  2342483282544\n"
     ]
    }
   ],
   "source": [
    "a = \"str\"\n",
    "b = a\n",
    "print('a = ', a, \"\\tid(a) = \", id(a))\n",
    "print('b = ', b, \"\\tid(b) = \", id(b))\n",
    "a.join('l')\n",
    "print('a = ', a, \"\\tid(a) = \", id(a))\n",
    "print('b = ', b, \"\\tid(b) = \", id(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "93cd8f30",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'中'"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'中'.encode('utf-8')\n",
    "b'\\xe4\\xb8\\xad'.decode('utf-8')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "eed1936e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " 3-01\n",
      "3.14\n"
     ]
    }
   ],
   "source": [
    "print('%2d-%02d' % (3, 1))\n",
    "print('%.2f' % 3.1415926)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "45f80217",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hello, world'"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'Hello, %s' % 'world'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "e16b2ecc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5050\n"
     ]
    }
   ],
   "source": [
    "sum = 0\n",
    "for i in range(1, 101):\n",
    "    sum += i;\n",
    "print(sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "e7e04185",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n"
     ]
    }
   ],
   "source": [
    "a = [1, 2, 3]\n",
    "for i in a:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "38265bf4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "3\n",
      "5\n",
      "7\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "n = 0\n",
    "while n < 10:\n",
    "    n = n + 1\n",
    "    if n % 2 == 0: # 如果n是偶数，执行continue语句\n",
    "        continue # continue语句会直接继续下一轮循环，后续的print()语句不会执行\n",
    "    print(n)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "4cfa230d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n"
     ]
    }
   ],
   "source": [
    "n = 1\n",
    "while n <= 100:\n",
    "    if n > 10: # 当n = 11时，条件满足，执行break语句\n",
    "        break # break语句会结束当前循环\n",
    "    print(n)\n",
    "    n = n + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "id": "b2580362",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Michael 95\n",
      "Bob 75\n",
      "Tracy 85\n",
      "Adam 67\n"
     ]
    }
   ],
   "source": [
    "d = {'Michael': 95, 'Bob': 75, 'Tracy': 85}\n",
    "d['Michael']\n",
    "d['Adam'] = 67\n",
    "for k, v in d.items():\n",
    "    print(k, v)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "b971ff5f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dstrfstrestrastrf\n"
     ]
    }
   ],
   "source": [
    "print(\"str\".join(\"dfeaf\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2eceeb20",
   "metadata": {},
   "outputs": [],
   "source": [
    "# *, 后面是命名关键字参数, *d 可变参数, **kw 关键字参数\n",
    "def f1(a, b, c=0, *, d, **kw):\n",
    "    print('a =', a, 'b =', b, 'c =', c, 'd =', d, 'kw =', kw)\n",
    "\n",
    "def f2(a, b, c=0, *e, d, **kw):\n",
    "    print('a =', a, 'b =', b, 'c =', c, 'd =', d, 'kw =', kw)\n",
    "\n",
    "# f2(1,2,3)\n",
    "args = (1, 2, 3)\n",
    "kw = {'d': 88, 'x': '#'}\n",
    "f1(*args, **kw)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "id": "bebb24f3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1,\n",
       " 3,\n",
       " 5,\n",
       " 7,\n",
       " 9,\n",
       " 11,\n",
       " 13,\n",
       " 15,\n",
       " 17,\n",
       " 19,\n",
       " 21,\n",
       " 23,\n",
       " 25,\n",
       " 27,\n",
       " 29,\n",
       " 31,\n",
       " 33,\n",
       " 35,\n",
       " 37,\n",
       " 39,\n",
       " 41,\n",
       " 43,\n",
       " 45,\n",
       " 47,\n",
       " 49,\n",
       " 51,\n",
       " 53,\n",
       " 55,\n",
       " 57,\n",
       " 59,\n",
       " 61,\n",
       " 63,\n",
       " 65,\n",
       " 67,\n",
       " 69,\n",
       " 71,\n",
       " 73,\n",
       " 75,\n",
       " 77,\n",
       " 79,\n",
       " 81,\n",
       " 83,\n",
       " 85,\n",
       " 87,\n",
       " 89,\n",
       " 91,\n",
       " 93,\n",
       " 95,\n",
       " 97,\n",
       " 99]"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "L = list(range(1, 100))[::2]\n",
    "L"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "id": "9660b342",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1,\n",
       " 3,\n",
       " 5,\n",
       " 7,\n",
       " 9,\n",
       " 11,\n",
       " 13,\n",
       " 15,\n",
       " 17,\n",
       " 19,\n",
       " 21,\n",
       " 23,\n",
       " 25,\n",
       " 27,\n",
       " 29,\n",
       " 31,\n",
       " 33,\n",
       " 35,\n",
       " 37,\n",
       " 39,\n",
       " 41,\n",
       " 43,\n",
       " 45,\n",
       " 47,\n",
       " 49,\n",
       " 51,\n",
       " 53,\n",
       " 55,\n",
       " 57,\n",
       " 59,\n",
       " 61,\n",
       " 63,\n",
       " 65,\n",
       " 67,\n",
       " 69,\n",
       " 71,\n",
       " 73,\n",
       " 75,\n",
       " 77,\n",
       " 79,\n",
       " 81,\n",
       " 83,\n",
       " 85,\n",
       " 87,\n",
       " 89,\n",
       " 91,\n",
       " 93,\n",
       " 95,\n",
       " 97,\n",
       " 99]"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(1,100,2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "id": "d1500394",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[9, 8, 7, 6, 5, 4, 3, 2, 1]"
      ]
     },
     "execution_count": 78,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 切片\n",
    "lst = list(range(1, 10))\n",
    "# lst[:]\n",
    "lst[::-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "id": "0cbfc8c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 A\n",
      "1 B\n",
      "2 C\n"
     ]
    }
   ],
   "source": [
    "# enumerate() 生成下标\n",
    "for i, value in enumerate(['A', 'B', 'C']):\n",
    "    print(i, value)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "id": "5316b905",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[4, 16, 36, 64, 100]"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 列表生成式\n",
    "[i*j for i in range(10) for j in range(0, i+1)]\n",
    "[x * x for x in range(1, 11) if x % 2 == 0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "12bf9122",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1\n",
      "2\n",
      "3\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "def fib(max):\n",
    "    n, a, b = 0, 0, 1\n",
    "    while n < max:\n",
    "        yield b\n",
    "        a, b = b, a + b\n",
    "        n = n + 1\n",
    "    return 'done'\n",
    "\n",
    "f = fib(5)\n",
    "for i in f:\n",
    "    print(i)\n",
    "# next(f)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "35378244",
   "metadata": {},
   "source": [
    "迭代器 generator\n",
    "next() 一步步迭代\n",
    "函数可以通过yield 变成迭代器函数\n",
    "StopIteration 超出迭代器范围"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dc178f22",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  },
  "vscode": {
   "interpreter": {
    "hash": "6a018d3a992d78c184ccc94ab54168c7b26325ed3c2283926339c3edbf5487e7"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
